//
//  pureImageViewController.m
//  YiXiu
//
//  Created by ZHY on 16/6/27.
//  Copyright © 2016年 zzz. All rights reserved.
//

#import "voiceViewController.h"
#import "AutoSizeTableViewCell.h"
#import "UserViewController.h"
#import "TextUserController.h"
#import "DetaiTextVC.h"
#import "CommModel.h"

@interface voiceViewController ()<UITableViewDelegate,UITableViewDataSource>

//可变数组在使用的时候一定要先初始化
@property (nonatomic, strong) NSMutableArray *cellModels;

@property (nonatomic, strong) UITableView  *tableView;

@property (nonatomic, assign) NSInteger  index;

@end

@implementation voiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.index = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.cellModels = [NSMutableArray array];
    
    
    [self.view addSubview:self.tableView];
    
    [self downGetData];
    
}



- (void)upGetData{
    /*
     dang	Int	第几页
     p_type	String	帖子类型
     */
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"dang"] = [NSString stringWithFormat:@"%ld",self.index];
    dic[@"p_type"] = @"1";
    
    
    [NetWorkingTool POST:@"post/getPostByType" parameters:dic success:^(id responseObject, NSError *error) {
        
        NSLog(@"------zzz----%@",[responseObject mj_JSONObject]);
        
        NSDictionary *dict = [responseObject mj_JSONObject];
        NSArray *dataArr = dict[@"json"];
        
        for (NSDictionary *dic in dataArr) {
            CommModel *model = [CommModel mj_objectWithKeyValues:dic];
            NSLog(@"u_name : %@",model.u_name);
            NSLog(@"u_name : %@",model.p_date);
            AutoSizeModel *model1 =  [AutoSizeModel tranfromByCommModel:model];
            [self.cellModels addObject:model1];
        }
        
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
        self.index++;
    } failure:^(id responseObject, NSError *error) {
        NSLog(@"error : %@",error);
        
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        [self.tableView.mj_header endRefreshingWithCompletionBlock:nil];
        
    }];
    
}



- (void)downGetData{
    /*
     dang	Int	第几页
     p_type	String	帖子类型
     */
    self.index = 1;
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"dang"] = [NSString stringWithFormat:@"%ld",self.index];
    dic[@"p_type"] = @"1";
    
    
    [NetWorkingTool POST:@"post/getPostByType" parameters:dic success:^(id responseObject, NSError *error) {
        
        NSLog(@"------zzz----%@",[responseObject mj_JSONObject]);
        
        NSDictionary *dict = [responseObject mj_JSONObject];
        NSArray *dataArr = dict[@"json"];
        
        
        [self.cellModels removeAllObjects];
        
        for (NSDictionary *dic in dataArr) {
            CommModel *model = [CommModel mj_objectWithKeyValues:dic];
            NSLog(@"u_name : %@",model.u_name);
            NSLog(@"u_name : %@",model.p_date);
            AutoSizeModel *model1 =  [AutoSizeModel tranfromByCommModel:model];
            [self.cellModels addObject:model1];
        }
        
        
        
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
        self.index++;
    } failure:^(id responseObject, NSError *error) {
        NSLog(@"error : %@",error);
        
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        [self.tableView.mj_header endRefreshingWithCompletionBlock:nil];
        
    }];
    
}




-(UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WidthOfView([UIScreen mainScreen]), HeightOfView([UIScreen mainScreen]) - 108) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        
        //下拉上拉刷新
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            [self upGetData];
        }];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self downGetData];
            
        }];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.cellModels.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AutoSizeTableViewCell * cells = [[AutoSizeTableViewCell alloc] initWithTableView:tableView];
    
    cells.model = self.cellModels[indexPath.row];
    
    [cells setIconBlock:^{
        NSLog(@"%s",__func__);
        
        UserViewController * testVC = [[UserViewController alloc]init];
        
//        [self.superVC.navigationController pushViewController:testVC animated:YES];
        
    }];
    
    [cells setTextBlock:^{
        DetaiTextVC * detaiVC = [[DetaiTextVC alloc]init];
        
//        [self.superVC.navigationController pushViewController:detaiVC animated:YES];
    }];
    
    
    return cells;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AutoSizeModel * model = self.cellModels[indexPath.row];
    
    return model.totalHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.selected = NO;
    
}

@end
